package com.hpeng.gl.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.service.Function;
import com.opensymphony.xwork2.ActionSupport;

@Component("assetCardoutputAction")
@Scope("prototype")
//申报管理状态下的资产导出action
public class AssetCardOutputExcelAction extends ActionSupport implements
		ServletResponseAware, ServletRequestAware {
	private static final long serialVersionUID = 1L;
	protected HttpServletRequest request = null;
	protected HttpServletResponse response = null;
	@Resource
	private AssetCardManager assetCardManager;
	//@Resource
	//private LoginUserMgr loginUserMgr;
	@Resource
	private Function function;
	
	@Override
	public String execute() {
		HttpServletRequest request = ServletActionContext.getRequest();
		//String userDepName=loginUserMgr.getUserDeptName(request);
		List<AssetCard> list = assetCardManager.getAllAssetCardByCondition(" from AssetCard where  acstatus like '1%'", " order by acid asc ");
		String[] columnNames = {"资产编号","资产名称","资产状态","资产来源","申报原因","申报部门","申报价值","生产日期","厂家","预计停用日期","实际停用日期","资产数量"};
		if (list != null) {
			int rows = 0;
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			WritableWorkbook book;
			try {
				//避免乱码
				WorkbookSettings setting = new WorkbookSettings();
			    Locale local = new Locale("zh","CN");
			    setting.setLocale(local);
			    setting.setEncoding("UTF-8");
			    Workbook.createWorkbook(os, setting);
				book = Workbook.createWorkbook(os);
				WritableSheet sheet = book.createSheet("第一页", rows); //设置工作薄当前页数
				for (int i = 0; i < list.size(); i++) {
					for(int j=0; j<columnNames.length; j++){
					//Label..第一个参数为列,第二个参数为行,第三个参数为值.
						Label label1 = new Label(j, 0, columnNames[j]);
						sheet.addCell(label1);
					}
					//输出
					Label label2 = new Label(0,i+1,list.get(i).getAcid());
					Label label3 = new Label(1,i+1,list.get(i).getAcname());
					
					Label label4 = null;
					String statusCode = list.get(i).getAcstatus().substring(0,1);
					String subCode = list.get(i).getAcstatus().substring(1);
					label4 = new Label(2,i+1,function.getStatusDesc(statusCode, subCode));
					
					
					Label label5 = new Label(3,i+1,list.get(i).getAssetcomefrom());
					Label label6 = new Label(4,i+1,list.get(i).getReportreason());
					Label label7 = new Label(5,i+1,list.get(i).getReportdepartment());
					Label label8 = new Label(6,i+1,list.get(i).getReportprice()+"");
					Label label9 = new Label(7,i+1,list.get(i).getProducer());
					Label label10 = new Label(8,i+1,list.get(i).getProducedate()+"");
					Label label11 = new Label(9,i+1,list.get(i).getPlanstopdate()+"");
					Label label12 = new Label(10,i+1,list.get(i).getActualstopdate()+"");
					Label label13 = new Label(11,i+1,list.get(i).getCount());
					sheet.addCell(label2);
					sheet.addCell(label3);
					sheet.addCell(label4);
					sheet.addCell(label5);
					sheet.addCell(label6);
					sheet.addCell(label7);
					sheet.addCell(label8);
					sheet.addCell(label9);
					sheet.addCell(label10);
					sheet.addCell(label11);
					sheet.addCell(label12);
					sheet.addCell(label13);
				}
				
				book.write();
				book.close();
				request.setAttribute("excelStream", new ByteArrayInputStream(os
						.toByteArray()));
				request.setAttribute("filename", "myexcel.xls");//设置文件名 
				return "success";
			} catch (IOException e) {
				e.printStackTrace();
				return "error";
			} catch (RowsExceededException e) {
				e.printStackTrace();
				return "error";
			} catch (WriteException e) {
				e.printStackTrace();
				return "error";
			}
		}else{
			System.out.println("统计数据不存在");
			return "error";
		}
	}
	public void setServletResponse(HttpServletResponse resp) {
		this.response = resp;
	}

	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	// 获取输出流
	public InputStream getInputExcel() {
		return (InputStream) request.getAttribute("excelStream");
	}

}
